<?php

namespace app\api\controller\apiinput;

use app\api\controller\Base;
use app\common\model\apiinput\Apishuqicalllistpush;
use app\common\model\apiinput\Apishuqicalldidsession;
use app\common\model\customer\Customervisit;
use think\facade\Request;
use app\api\controller\Pub;

class Shuqi extends Base
{
    /**
     * https://cdn.bdsaas.com/doc/cloudcall.pdf
     * 数企 api对接
     * 目前：获取话单信息
     * 7001 开发者呼叫频次管控 02 应用呼叫频次 03 固话号码呼叫频次 04 黑名单呼叫管控  
     * 7108 用户状态冻结 8003 振铃超时  04 振铃挂机 09 接通前主叫挂机 10超过通话时长 8100 被叫号码不存在 
     * 8101 被叫无应答 02用户正忙 03暂时无法接通 04已关机 05被叫挂机或被叫已停机 9999其他异常
     */


    public function initialize()
    {
        parent::initialize();
        // $this->app_key = get_config_value('call_center_app_key');
        // $this->code = get_config_value('call_center_code');
    }
    /**
     * 话单推送
     * @return [type] [description]
     */
    public function call_list_push(){
        $pwd = input('pwd');
        if(empty($pwd)||$pwd!==get_config_value('self_api_key_shuqi')){
            return moerr('参数错误');
        }
        $data = input('post.');
        if(empty($data['sessionId'])){
            return moerr('缺少关键信息');
        }
        $nsq = new Apishuqicalllistpush;
        $nsq = $nsq->allowField(true)->save($data);
        return json_encode(['resultCode'=>0]);
    }

    /**
     * 通话结果推送
     * @return [type] [description]
     */
    public function call_did_sessionId(){
        $pwd = input('pwd');
        if(empty($pwd)||$pwd!==get_config_value('self_api_key_shuqi')){
            return moerr('参数错误');
        }
        $data = input('post.');
        if(empty($data['data']['callId'])||empty($data['data']['sessionId'])){
            return moerr('缺少关键信息');
        }
        $cv = new Customervisit;
        $cv = $cv->where('CallsessionId',$data['data']['callId'])->find();
        if(!empty($cv)){
            $cv->CallsessionId = $data['data']['sessionId'];
            $cv->save();
        }
        return json_encode(['resultCode'=>0]);
    }


    /**
     * 通话结果推送
     * @return [type] [description]
     */
    public function call_did_sessionId_save(){
        $pwd = input('pwd');
        if(empty($pwd)||$pwd!==get_config_value('self_api_key_shuqi')){
            return moerr('参数错误');
        }
        $data = input('post.');
        if(empty($data['data']['callId'])||empty($data['data']['sessionId'])){
            return moerr('缺少关键信息');
        }
        $cv = new Apishuqicalldidsession;
        $cv->sessionId = $data['data']['sessionId'];
        $cv->callId = $data['data']['callId'];
        $cv->save();
        return json_encode(['resultCode'=>0]);
    }


    private function msg($data)
    {
        return [
            'code' => true,
            'msg' => 'success',
            'data' => $data
        ];
    }

    public function request($path, $data = [])
    {
        $url = 'https://openapi.bdsaas.com/' . $path;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        if ($data) {
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        if (!$response || $code != 200) {
            //错误强制返回
            return moerr('电话模块通讯失败，请联系管理员');
        }
        return json_decode($response, true);
    }
}